package com.longcai.service.impl.customer;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjAddress;
import com.longcai.mapper.customer.ClyjAddressMapper;
import com.longcai.service.customer.ClyjAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-03-25
 */
@Service
public class ClyjAddressServiceImpl extends ServiceImpl<ClyjAddressMapper, ClyjAddress> implements ClyjAddressService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjAddressMapper clyjAddressMapper;

    /**
     * 新增收货地址
     *
     * @param userid
     * @param params
     * @return
     */
    @Override
    public ApiResult<ClyjAddress> insertaddress(String userid, JSONObject params) {
        try {
            //获取参数
            String name = params.getString("name");
            String phone = params.getString("phone");
            String province = params.getString("province");
            String city = params.getString("city");
            String county = params.getString("county");
            String detail = params.getString("detail");
            String status = params.getString("status");
            String sex = params.getString("sex");
            String code=params.getString("code");
            ClyjAddress clyjAddress = new ClyjAddress();
            clyjAddress.setAddressId(UUID.randomUUID().toString().replace("-", ""));
            clyjAddress.setUserId(userid);
            clyjAddress.setReceiveName(name);
            clyjAddress.setReceivePhone(phone);
            clyjAddress.setAddressProvince(province);
            clyjAddress.setAddressCity(city);
            clyjAddress.setAddressCounty(county);
            clyjAddress.setAddressDetail(detail);
            clyjAddress.setSex(sex);
            clyjAddress.setCode(code);
            if (status.equals("1")) {
                //设置为默认时，去查询数据库 把该用户其他默认地址改为正常状态
                QueryWrapper<ClyjAddress> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", userid);
                queryWrapper.eq("address_status", "1");
                ClyjAddress clyjAddress1 = new ClyjAddress();
                clyjAddress1.setAddressStatus("0");
                this.clyjAddressMapper.update(clyjAddress1, queryWrapper);
                clyjAddress.setAddressStatus("1");
            } else {
                clyjAddress.setAddressStatus("0");
            }
            Integer integer = this.clyjAddressMapper.insert(clyjAddress);
            logger.debug("插入成功+++++++", integer);
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.debug("插入失败+++++++");
            e.printStackTrace();
            return ApiResult.fail("插入失败"+e.getMessage());
        }
    }

    /**
     * 修改收货地址
     *
     * @param userid
     * @param addressid
     * @param params
     * @return
     */
    @Override
    public ApiResult<ClyjAddress> updateaddress(String userid, String addressid, JSONObject params) {
        try {
            //获取参数
            String name = params.getString("name");
            String phone = params.getString("phone");
            String province = params.getString("province");
            String city = params.getString("city");
            String county = params.getString("county");
            String detail = params.getString("detail");
            String status = params.getString("status");
            String sex = params.getString("sex");
            String code=params.getString("code");
            ClyjAddress clyjAddress = this.clyjAddressMapper.selectById(addressid);
            clyjAddress.setUserId(userid);
            clyjAddress.setReceiveName(name);
            clyjAddress.setReceivePhone(phone);
            clyjAddress.setAddressProvince(province);
            clyjAddress.setAddressCity(city);
            clyjAddress.setAddressCounty(county);
            clyjAddress.setAddressDetail(detail);
            clyjAddress.setSex(sex);
            clyjAddress.setCode(code);
            if (status.equals("1")) {
                //设置为默认时，去查询数据库 把该用户其他默认地址改为正常状态
                QueryWrapper<ClyjAddress> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", userid);
                queryWrapper.eq("address_status", "1");
                ClyjAddress clyjAddress1 = new ClyjAddress();
                clyjAddress1.setAddressStatus("0");
                this.clyjAddressMapper.update(clyjAddress1, queryWrapper);
                clyjAddress.setAddressStatus("1");
            } else {
                clyjAddress.setAddressStatus("0");
            }
            Integer integer = this.clyjAddressMapper.updateById(clyjAddress);
            logger.debug("修改成功+++++++++++", integer);
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.debug("修改失败+++++++++++");
            e.printStackTrace();
            return ApiResult.fail("修改失败"+e.getMessage());
        }
    }

    /**
     * 删除地址
     *
     * @param params
     * @return
     */
    @Override
    public ApiResult<ClyjAddress> deladdress(JSONObject params) {
        try {
            String addressid = params.getString("addressid");
            ClyjAddress clyjAddress = clyjAddressMapper.selectById(addressid);
            clyjAddress.setAddressStatus("3");
            Integer integer = this.clyjAddressMapper.updateById(clyjAddress);
            logger.debug("修改成功+++++++++++", integer);
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.debug("修改失败+++++++++++");
            e.printStackTrace();
            return ApiResult.fail("修改失败"+e.getMessage());
        }
    }

    /**
     * 获取地址信息
     *
     * @param params
     * @return
     */
    @Override
    public ApiResult<ClyjAddress> getAdsInfo(JSONObject params) {
        try {
            String addressid = params.getString("addressid");
            ClyjAddress clyjAddress = clyjAddressMapper.selectById(addressid);
            logger.debug("查询成功", clyjAddress);
            return ApiResult.success(clyjAddress);
        } catch (Exception e) {
            logger.debug("查询失败");
            e.printStackTrace();
            return ApiResult.fail("修改失败"+e.getMessage());
        }
    }

    /**
     * 地址列表
     *
     * @param userid
     * @return
     */
    @Override
    public ApiResult getaddress(String userid) {
        try {
            QueryWrapper<ClyjAddress> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("address_status", "0").or().eq("address_status", "1");
            queryWrapper.orderByDesc("address_status");
            List<ClyjAddress> list = this.clyjAddressMapper.selectList(queryWrapper);
            logger.debug("查询成功++++++++++", list);
            return ApiResult.success(list);
        } catch (Exception e) {
            logger.debug("查询失败+++++++++++");
            e.printStackTrace();
            return ApiResult.fail("修改失败"+e.getMessage());
        }
    }
}
